﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PopupNumKB;

namespace fap
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class 运动控制首页 : UserControl
    {

        TextBox m_selTxtBox;
        public 运动控制首页()
        {
            InitializeComponent();
        }



        #region 数据申明



        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button obj = (Button)sender;

                switch (obj.Tag)
                {
                    case "写入":
                        {
                            更新();
                            运动控制数据.保存();
                            break;
                        }
                }
                this.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        public void 更新()
        {
           
            运动控制数据.参数写入数值[0].显示数值 = Convert.ToDouble(运动节拍显示.Text);
            运动控制数据.参数写入数值[1].数值 =  (int) Convert.ToDouble(底板放入个数显示.Text);
            运动控制数据.参数写入数值[2].显示数值 = Convert.ToDouble(推动1原位延时.Text); 
            运动控制数据.参数写入数值[3].显示数值 = Convert.ToDouble(推动1到位延时.Text); 
            运动控制数据.参数写入数值[4].显示数值 = Convert.ToDouble(推动2原位延时.Text); 
            运动控制数据.参数写入数值[5].显示数值 = Convert.ToDouble(推动2到位延时.Text); 
            运动控制数据.参数写入数值[6].显示数值 = Convert.ToDouble(推动3原位延时.Text); 
            运动控制数据.参数写入数值[7].显示数值 = Convert.ToDouble(推动3到位延时.Text); 
            运动控制数据.参数写入数值[8].显示数值 = Convert.ToDouble(推动4原位延时.Text); 
            运动控制数据.参数写入数值[9].显示数值 = Convert.ToDouble(推动4到位延时.Text); 
            运动控制数据.参数写入 = true;
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            BindPreviewMouseUpEvent();

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            运动节拍显示.DataContext = 运动控制数据.参数写入数值[0];
            底板放入个数显示.DataContext = 运动控制数据.参数写入数值[1];
            推动1原位延时.DataContext = 运动控制数据.参数写入数值[2];
            推动1到位延时.DataContext = 运动控制数据.参数写入数值[3];
            推动2原位延时.DataContext = 运动控制数据.参数写入数值[4];
            推动2到位延时.DataContext = 运动控制数据.参数写入数值[5];
            推动3原位延时.DataContext = 运动控制数据.参数写入数值[6];
            推动3到位延时.DataContext = 运动控制数据.参数写入数值[7];
            推动4原位延时.DataContext = 运动控制数据.参数写入数值[8];
            推动4到位延时.DataContext = 运动控制数据.参数写入数值[9];
        }

        private void 按钮点击(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            switch (button.Tag)
            {

                case "保存":
                    {

                        break;
                    }
            }
        }



        #region 参数输入
        private void BindPreviewMouseUpEvent()
        {

            grqNumKB.RecvData += new NumericKeyboard.RecvDataEventHandler(grqNumKB_RecvData);
        }
        private void textBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            popNumKeyboard.IsOpen = false;

            m_selTxtBox = sender as TextBox;
            popNumKeyboard.PlacementTarget = m_selTxtBox;   //表示Popup控件的放置的位置依赖的对象
            popNumKeyboard.IsOpen = true;
        }
        private void grqNumKB_RecvData(object sender, int nDig)
        {
            if (m_selTxtBox == null)
            {
                return;
            }

            if (nDig == 0x08)
            {
                //回退
                if (!string.IsNullOrEmpty(m_selTxtBox.Text))
                {

                    m_selTxtBox.Text = m_selTxtBox.Text.Substring(0, m_selTxtBox.Text.Length - 1);
                }
            }
            else if(nDig == 0x2E)
            {
                m_selTxtBox.Text += ".";
            }
            else if (nDig == 0x13)
            {
                popNumKeyboard.IsOpen = false;
            }
            else
            {
                int n = nDig - 0x30;
                if (string.IsNullOrEmpty(m_selTxtBox.Text))
                {
                    m_selTxtBox.Text = n.ToString();
                }
                else
                {
                    m_selTxtBox.Text += n.ToString();
                }
            }
        }
        #endregion


    }

    /// <summary>
    /// BulletCheckBox.xaml 的交互逻辑
    /// </summary>
    public class BulletCheckBox : CheckBox
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("Off"));
        /// <summary>
        /// 默认文本（未选中）
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.Register(
            "CheckedText", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("On"));
        /// <summary>
        /// 选中状态文本
        /// </summary>
        public string CheckedText
        {
            get { return (string)GetValue(CheckedTextProperty); }
            set { SetValue(CheckedTextProperty, value); }
        }

        public static readonly DependencyProperty CheckedForegroundProperty =
            DependencyProperty.Register("CheckedForeground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.WhiteSmoke));
        /// <summary>
        /// 选中状态前景样式
        /// </summary>
        public Brush CheckedForeground
        {
            get { return (Brush)GetValue(CheckedForegroundProperty); }
            set { SetValue(CheckedForegroundProperty, value); }
        }

        public static readonly DependencyProperty CheckedBackgroundProperty =
            DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.LimeGreen));
        /// <summary>
        /// 选中状态背景色
        /// </summary>
        public Brush CheckedBackground
        {
            get { return (Brush)GetValue(CheckedBackgroundProperty); }
            set { SetValue(CheckedBackgroundProperty, value); }
        }

        static BulletCheckBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BulletCheckBox), new FrameworkPropertyMetadata(typeof(BulletCheckBox)));
        }
    }

}
